//
//  StrategyDetailViewController.m
//  课余时间
//
//  Created by zgr on 16/4/9.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "StrategyDetailViewController.h"
#import "UIView+SDAutoLayout.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "StraDetailTableViewCell.h"
#import "SectionsModel.h"
#import "UIImageView+WebCache.h"
@interface StrategyDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)CGFloat spaceH;

@end

@implementation StrategyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[StraDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sectionCount = [_strategyModel.childrenModel count];
    
    return sectionCount;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_strategyModel.childrenModel[section] sectionModel] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StraDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
    SectionsModel *model = [_strategyModel.childrenModel[indexPath.section] sectionModel][indexPath.row];
     cell.model = model;
    NSLog(@"======%ld",model.photoModel.count);
    
   if (model.photoModel.count > 0) {
      
    
        cell.backView.frame = CGRectMake(0, _spaceH, KWIDTH, 200 *model.photoModel.count);
        [cell.contentView addSubview:cell.backView];
    for (int i = 0; i < model.photoModel.count; i++) {
    
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 200 * i + 2 *(i + 1), KWIDTH - 10, 200)];
        [image sd_setImageWithURL:[NSURL URLWithString:[model.photoModel[i] image_url]]];
        [cell.backView addSubview:image];
        
                }
        
        return cell;
    }
    else{
        
       [cell.backView removeFromSuperview];
       
    }
   // cell.layer.masksToBounds = YES;
   
    NSLog(@"%f",cell.height);
    
      return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return [_strategyModel.childrenModel[section]title];

    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    Class currentClass = [StraDetailTableViewCell class];
    SectionsModel *model = [_strategyModel.childrenModel[indexPath.section] sectionModel][indexPath.row];

    _spaceH = [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:currentClass contentViewWidth:[self cellContentWith]];
    return  [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:currentClass contentViewWidth:[self cellContentWith]] + 200 * model.photoModel.count ;
    
   
}
-(CGFloat)cellContentWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
